package com.deer.wms.base.system.web.zhBusiness.mestest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.deer.wms.base.system.model.task.TaskInfoDto;
import com.deer.wms.base.system.model.zh.*;
import com.deer.wms.base.system.service.zh.ZhMesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 李帅辰
 * @date 2021/6/11 10:32
 */
@Controller
@RequestMapping("/MES")
@Slf4j
public class ZhMesController {
    @Autowired
    private ZhMesService zhMesService;

    /**
     * WMS同步单晶信息给MES
     * NMMaterialInfo
     ***/
    @PostMapping("/SysnBoxItem")
    @ResponseBody
    public String SysnBoxItem(@RequestBody  ZhBox zhBox) {
        log.info("WMS同步单晶信息给MES:{}",zhBox.toString());
        return zhMesService.SysnBoxItem(zhBox);
    }
    /**
     * NMStockStateSync
     * WMS同步缓存线状态给MES，NG或者OK
     * **/
    @PostMapping("/CacheLineMes")
    @ResponseBody
    public String CacheLineMes(@RequestBody CacheLine cacheLine) throws Exception {
       return zhMesService.CacheLineMes(cacheLine);
    }

    /**
     * NMMaterialInspectData
     * WMS同步晶棒检验数据给MES
     * **/
    @PostMapping("/QualityMes")
    @ResponseBody
    public String QualityMes(@RequestBody Quality quality) throws Exception {
       return zhMesService.QualityMes(quality);
    }

    /**
     * NMStockInDJK
     * 晶棒入缓存线，发送位置信息给MES
     * **/
    @PostMapping("/CacheLineLocationMes")
    @ResponseBody
    public String CacheLineLocationMes(@RequestBody CacheLine cacheLine) throws Exception {
        return zhMesService.CacheLineLocationMes(cacheLine);
    }

    /**
     * NMInStockInfo
     * 晶棒缓存线同步库存信息给MES
     * **/
    @PostMapping("/billInUpdateBox")
    @ResponseBody
    public String BillInUpdateBoxMes(@RequestBody BillInUpdateBox billInUpdateBox){
        return zhMesService.BillInUpdateBoxMes(billInUpdateBox);
    }

    /**
     * NMSendStockOutDJK
     * MES粘棒派工请求
     * **/
    @PostMapping("/billOut")
    @ResponseBody
    public ZhResult billOut(@RequestBody ZhParam zhParam){
        String jsonString = zhParam.getJsonString();
//        List<ZhBillOutModel> zhBillOutModels = JSONObject.parseArray(jsonString, ZhBillOutModel.class);
        ZhBillOutModel zhBillOutModel = JSONObject.parseObject(jsonString,ZhBillOutModel.class);
            String materialId = zhBillOutModel.getMaterialId();
            ZhResult zhResult = new ZhResult();
            zhResult.setId(materialId);
            zhResult.setCode(0);
            zhResult.setMsg("接受成功");
            SimpleDateFormat df_12=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1=new Date();
            zhResult.setDate(df_12.format(date1));
            log.error("接收MES出库请求成功:内容:{}",zhParam.toString());
        return zhResult;
    }


    /**
     * NMStockOutDJK
     * 单晶库出库完成，WMS上报出库完成
     * **/
    @PostMapping("/CancelAccount")
    @ResponseBody
    public String CancelAccount(@RequestBody CacheLine cacheLine) {
        return zhMesService.CancelAccount(cacheLine);
    }

    @PostMapping("/GlueStickRoomMes")
    @ResponseBody
    public  ZhResult GlueStickRoomMes(@RequestBody ZhParam zhParam) {
        String jsonString = zhParam.getJsonString();
       // List<GlueStickRoom> glueStickRooms = JSONObject.parseArray(jsonString, GlueStickRoom.class);
        GlueStickRoom glueStickRoom = JSONArray.parseObject(jsonString, GlueStickRoom.class);//获取到任务
            String materialId = glueStickRoom.getLotId();
            ZhResult zhResult = new ZhResult();
            zhResult.setId(materialId);
            zhResult.setCode(0);
            zhResult.setMsg("接受成功");
            SimpleDateFormat df_12=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date1=new Date();
            zhResult.setDate(df_12.format(date1));
        return zhResult;
    }


    /**
     * 查询成品托信息测试接口
     * @author 申紫微
     * @date 14:30 2021/6/18
     * @params carrierId
     * @return java.lang.String
     **/
    @GetMapping("/NMCarrierInfo")
    @ResponseBody
    public String selectFinishProduct(String carrierId){
        ZhResultMESFinishProduct zhResultMESFinishProduct = zhMesService.SelectFinishProduct(carrierId);
        return JSONObject.toJSONString(zhResultMESFinishProduct);
    }

    /**
     * WMS成品入库，同步入库信息给MES NMStockInCPK
     * @author 申紫微
     * @date 17:04 2021/6/18
     * @params param
     * @return java.lang.String
     **/
    @PostMapping("NMStockInCPK")
    @ResponseBody
    public String NMStockInCPK(@RequestBody ZhParamSyncMESFinishProductInStorage param){
        String res = zhMesService.NMStockInCPK(param);
        return res;
    }

    /**
     * NMSendCarrierInfo MES下发包装线入库物料信息
     * @author 申紫微
     * @date 17:10 2021/6/18
     * @params
     * @return java.lang.String
     **/
    @PostMapping("NMSendCarrierInfo")
    @ResponseBody
    public String NMSendCarrierInfo(@RequestBody ZhParam param){
        String jsonString = param.getJsonString();
        ZhResultMESSendCarrierInfo model = JSONArray.parseObject(jsonString, ZhResultMESSendCarrierInfo.class);//获取到任务
        ZhResult zhResult = new ZhResult();
        zhResult.setCode(0);
        zhResult.setId(model.getCarrierId());
        zhResult.setMsg("接受成功");
        SimpleDateFormat df_12=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1=new Date();
        zhResult.setDate(df_12.format(date1));
        return JSONObject.toJSONString(zhResult);
    }

    /**
     * WMS通知设备启动或者停止
     * @author 申紫微
     * @date 10:19 2021/6/19
     * @params
     * @return java.lang.String
     **/
    @PostMapping("NMRunDevice")
    @ResponseBody
    public String NMRunDevice(@RequestBody ZhParamMESRunDevice param){
        String res = zhMesService.NMRunDevice(param);
        return res;
    }

    /**
     * WMS通知RGV设备的状态
     * @author 申紫微
     * @date 10:20 2021/6/19
     * @params
     * @return java.lang.String
     **/
    @PostMapping("NMRGVDeviceState")
    @ResponseBody
    public String NMRGVDeviceState(){

        return null;
    }


    /**
     *INT_MES_WMS_10  WMS修改NG，预留
     * @author 李帅辰
     * @date 2021/6/18 18:44
     * @return java.lang.String
     */
    @PostMapping("NMChangeNG")
    @ResponseBody
    public String NMChangeNG(@RequestBody ShelfPlan shelfPlan) {
        return  zhMesService.NMChangeNG(shelfPlan);
    }


    /**
     * INT_MES_WMS_11  MES修改NG，预留
     * @author 李帅辰
     * @date 2021/6/18 18:45
     * @return java.lang.String
     */
    @PostMapping("NMChangeNGMEsToWms")
    @ResponseBody
    public ZhResult NMChangeNGMEsToWms(@RequestBody ZhParam zhParam) {
        return zhMesService.NMChangeNGMEsToWms(zhParam);
    }

}
